package dao

import (
	"miao-wails/backed/structs/tables"
)

var SecretKeyDao = new(secretKeyDao)

type secretKeyDao struct{}

func (d secretKeyDao) CreateKey(m tables.SecretKey) (err error) {
	err = DB.Save(&m).Error
	return
}

func (d secretKeyDao) FindKeys() (li []tables.SecretKey, err error) {
	err = DB.Model(&tables.SecretKey{}).Find(&li).Error
	return
}
func (d secretKeyDao) FindKeyById(id int64) (m tables.SecretKey, err error) {
	err = DB.Model(&tables.SecretKey{}).Where("id = ?", id).First(&m).Error
	return
}

func (d secretKeyDao) DelKeyById(id int64) (err error) {
	err = DB.Where("id = ?", id).Delete(&tables.SecretKey{}).Error
	return
}

func (d secretKeyDao) CreateText(m tables.SecretText) (err error) {
	err = DB.Create(&m).Error
	return
}
func (d secretKeyDao) FindTextList(isPrivate bool) (li []tables.SecretText, err error) {
	err = DB.Model(&tables.SecretText{}).Where("is_private = ?", isPrivate).Find(&li).Error
	return
}
func (d secretKeyDao) DelTextById(id int64) (err error) {
	err = DB.Where("id = ?", id).Delete(&tables.SecretText{}).Error
	return
}
func (d secretKeyDao) UpdateTextById(m tables.SecretText) (err error) {
	err = DB.Save(&m).Error
	return
}
func (d secretKeyDao) FindTextById(id int64) (m tables.SecretText, err error) {
	err = DB.Where("id = ?", id).First(&m).Error
	return
}

// CountTextMapKey 统计某个key 关联了多少个密文
func (d secretKeyDao) CountTextMapKey(kid int64) (num int64, err error) {
	err = DB.Model(&tables.SecretText{}).Where("key_id = ?", kid).Count(&num).Error
	return
}

func (d secretKeyDao) CreateChapter(m tables.SecretChapter) (err error) {
	err = DB.Create(&m).Error
	return
}

func (d secretKeyDao) FindChapterList(tid int64) (li []tables.SecretChapter, err error) {
	err = DB.Model(&tables.SecretChapter{}).Where("text_id = ?", tid).Find(&li).Error
	return
}
func (d secretKeyDao) DelChapterByTextId(tid int64) (err error) {
	err = DB.Where("text_id = ?", tid).Delete(&tables.SecretChapter{}).Error
	return
}

func (d secretKeyDao) DelChapterById(id int64) (err error) {
	err = DB.Where("id = ?", id).Delete(&tables.SecretChapter{}).Error
	return
}
